package com.cdinternationalcompany.syhapp.push

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.SystemClock
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.cdinternationalcompany.syhapp.MainActivity
import com.cdinternationalcompany.syhapp.R
import com.cdinternationalcompany.syhapp.push.Constants.Companion.tag
import org.json.JSONObject

class NotificationHelper {

    companion object {
        private val TAG = "$tag.${NotificationHelper::class.java.simpleName}"
        fun showNotification(
            context: Context?, title: String, content: String, extra: String
        ) {

            val jumpIntent = Intent(context, MainActivity::class.java)
            jumpIntent.putExtra("extras", extra)
            val pendingIntent = PendingIntent.getActivity(
                context, 0, jumpIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            val builder = NotificationCompat.Builder(context!!, "1")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)

            val notificationManager = NotificationManagerCompat.from(context)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //创建通知渠道
                val name: CharSequence = context.resources.getString(R.string.app_name)
                val description = context.resources.getString(R.string.app_name)
                val importance = NotificationManager.IMPORTANCE_HIGH //重要性级别 这里用默认的
                val mChannel = NotificationChannel("1", name, importance)
                mChannel.description = description //渠道描述
                mChannel.enableLights(true) //是否显示通知指示灯
                mChannel.enableVibration(true) //是否振动
//                mChannel.setSound(Notification.DEFAULT_SOUND,null)
//                mChannel.setD
                notificationManager.createNotificationChannel(mChannel) //创建通知渠道
            }
            notificationManager.notify(SystemClock.elapsedRealtime().toInt(), builder.build())
            Log.d(TAG, "showNotification: OK")
            return
        }

        fun openPage(content: String, ctx: Context?) {
            val jumpIntent = Intent(ctx, MainActivity::class.java)
            jumpIntent.putExtra("extras", content)
            jumpIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            ctx?.startActivity(jumpIntent)
        }
    }

}